1 using UnityEngine;
2 using
System.Collections.Generic;
3 using
System.Collections;
4
5 public
class Grid : MonoBehaviour {
6
7     
[SerializeField]
8     
private GameObject tilePrefab;
9     
[SerializeField]
10     
private GameObject tilePrefab2;
11
12     
[SerializeField]
13     
private int rows;
14     
[SerializeField]
15     
private int cols;
16     
//[SerializeField]
17     
//private int height;
18     
[SerializeField]
19     
private bool checkDiagonals = true;
20
21     
[SerializeField]
22     
private GameObject[] piecesPrefabs;
23
24     
private Node[,] grid;
25     
private Vector3 tileSize;
26     
private Vector3 size;
27
28     
private bool piecesSpawned;
29
30     
public int Rows {
31         
get {return rows;}
32     }
33
34     
public int Cols {
35         
get {return cols;}
36     }
37
38     
public bool ArePiecesSpawned {
39         
get {
40             
return piecesSpawned;
41         }
42     }
43
44     
public bool IsReady {
45         
get{
46             
for (int row = 0; row < rows; row++) {
47                 
for (int col = 0; col < cols; col++) {
48                     Node node = grid[row,col];
49                     
if (!node.IsReady) return false;
50                 }
51             }
52
53             
return true;
54         }
55     }
56
57     
public int Size {
58         
get {
59             
return rows * cols;
60         }
61     }
62
63     
public Node GetNodeAt(int row, int col) {
64         
if (row < 0 || row >= rows || col < 0 || col >= cols) return null;
65         
return grid[row,col];
66     }
67
68     
public Node GetNodeAt(Vector3 pos) {
69         pos -= transform.position;
70
71         
float percentRow = (pos.z / size.z) + 0.5f;
72         
float percentCol = (pos.x / size.x) + 0.5f;
73         percentRow = Mathf.Clamp01(percentRow);
74         percentCol = Mathf.Clamp01(percentCol);
75         
int row = Mathf.RoundToInt((rows-1) * percentRow);
76         
int col = Mathf.RoundToInt((cols-1) * percentCol);
77
78         
return grid[row,col];
79     }
80
81     
void Awake() {
82         grid =
new Node[rows,cols];
83         tileSize = tilePrefab.GetComponent<Renderer>().bounds.size;
84         size =
new Vector3(tileSize.x * cols, tileSize.y, tileSize.z * rows);
85         CreateGrid();
86     }
87
88     
void CreateGrid() {
89         Vector3 bottomLeft =
new Vector3(
90                 transform.position.x - size.x /
2 + tileSize.x / 2,
91                 transform.position.y,
92                 transform.position.z - size.z /
2 + tileSize.z / 2);
93         Vector3 startPosition = bottomLeft;
94
95         GameObject tile = tilePrefab;
96
97         
for (int row = 0; row < rows; row++) {
98             
for (int col = 0; col < cols; col++) {
99                 startPosition.z = bottomLeft.z + tileSize.z * row;
100                 startPosition.x = bottomLeft.x + tileSize.x * col;
101                 GameObject go = Instantiate(tile, startPosition, tile.transform.rotation)
as GameObject;
102                 Node dn = go.AddComponent<Node>();
103                 dn.row = row;
104                 dn.col = col;
105                 dn.rowChess = Converter.ToChessRow(row);
106                 dn.colChess = Converter.ToChessCol(col);
107                 grid[row,col] = dn;
108                 go.transform.parent = transform;
109                 go.transform.localScale = Vector3.zero;
110
111                 dn.ScaleIn(Random.Range(
0f,1f),Random.Range(1f,2f),tile.transform.localScale);
112                 tile = SwapTilePrefab(tile);
113             }
114             tile = SwapTilePrefab(tile);
115         }
116
117         StartCoroutine(SpawnPieces());
118     }
119
120     GameObject SwapTilePrefab(GameObject go) {
121         
if (tilePrefab == go) return tilePrefab2;
122         
123         
return tilePrefab;
124     }
125
126     IEnumerator SpawnPieces() {
127         
while (!IsReady) {
128             
yield return null;
129         }
130
131         
//0 - box
132         
//1 - triangle
133         
//2 - circle
134         
//3 - cross
135         
//4 - hexagon
136
137         PlayerType p1T = PlayerType.P1;
138         PlayerType p2T = PlayerType.P2;

139
140         ///
*----------------------CHESS sample
141         //spawn circles
142         
for (int i = 0; i <= 7; i++) {
143             SpawnPiece(
new GridCoords(1,i),piecesPrefabs[2], p1T); // p1 circ
144             SpawnPiece(
new GridCoords(6,i),piecesPrefabs[2], p2T); // p2 circ
145         }
146
147         //spawn boxes
148         SpawnPiece(
new GridCoords(0,0),piecesPrefabs[0], p1T); //p1 box
149         SpawnPiece(
new GridCoords(0,7),piecesPrefabs[0], p1T); //p1 box
150         SpawnPiece(
new GridCoords(7,0),piecesPrefabs[0], p2T); //p2 box
151         SpawnPiece(
new GridCoords(7,7),piecesPrefabs[0], p2T); //p2 box
152
153         //spawn triangles
154         SpawnPiece(
new GridCoords(0,2),piecesPrefabs[1], 180, p1T); //p1 tri
155         SpawnPiece(
new GridCoords(0,5),piecesPrefabs[1], 180, p1T); //p1 tri
156         SpawnPiece(
new GridCoords(7,2),piecesPrefabs[1], p2T); //p2 tri
157         SpawnPiece(
new GridCoords(7,5),piecesPrefabs[1], p2T); //p2 tri
158
159         //spawn crosses
160         SpawnPiece(
new GridCoords(0,4),piecesPrefabs[3], p1T); //p1 cross
161         SpawnPiece(
new GridCoords(7,4),piecesPrefabs[3], p2T); //p2 cross
162
163         //spawn hexagons
164         SpawnPiece(
new GridCoords(0,3),piecesPrefabs[4], p1T); //p1 hex
165         SpawnPiece(
new GridCoords(7,3),piecesPrefabs[4], p2T); //p2 hex
166
167         //spawn rectangles - knights
for testing
168         SpawnPiece(
new GridCoords(0,1),piecesPrefabs[5], p1T); //p1 rect
169         SpawnPiece(
new GridCoords(0,6),piecesPrefabs[5], p1T); //p1 rect
170         SpawnPiece(
new GridCoords(7,1),piecesPrefabs[5], p2T); //p2 rect
171         SpawnPiece(
new GridCoords(7,6),piecesPrefabs[5], p2T); //p2 rect
172         //*/

173
174
175         
/*------------------GC
176         
for (int i = 1; i <= 6; i++) {
177             SpawnPiece(
new GridCoords(2,i),piecesPrefabs[2], p1T); // p1 circ
178             SpawnPiece(
new GridCoords(5,i),piecesPrefabs[2], p2T); // p2 circ
179         }
180
181         //spawn boxes
182         SpawnPiece(
new GridCoords(1,1),piecesPrefabs[0], p1T); //p1 box
183         SpawnPiece(
new GridCoords(1,6),piecesPrefabs[0], p1T); //p1 box
184         SpawnPiece(
new GridCoords(6,1),piecesPrefabs[0], p2T); //p2 box
185         SpawnPiece(
new GridCoords(6,6),piecesPrefabs[0], p2T); //p2 box
186
187         //spawn triangles
188         SpawnPiece(
new GridCoords(1,2),piecesPrefabs[1], 180, p1T); //p1 tri
189         SpawnPiece(
new GridCoords(1,5),piecesPrefabs[1], 180, p1T); //p1 tri
190         SpawnPiece(
new GridCoords(6,2),piecesPrefabs[1], p2T); //p2 tri
191         SpawnPiece(
new GridCoords(6,5),piecesPrefabs[1], p2T); //p2 tri
192
193         //spawn crosses
194         SpawnPiece(
new GridCoords(1,4),piecesPrefabs[3], p1T); //p1 cross
195         SpawnPiece(
new GridCoords(6,4),piecesPrefabs[3], p2T); //p2 cross
196
197         //spawn hexagons
198         SpawnPiece(
new GridCoords(1,3),piecesPrefabs[4], p1T); //p1 hex
199         SpawnPiece(
new GridCoords(6,3),piecesPrefabs[4], p2T); //p2 hex
200
201         */

202
203         piecesSpawned =
true;
204     }
205
206     
public void SpawnPiece(GridCoords coords, GameObject piece, float yRotation, PlayerType playerType) {
207         Node pieceNode = GetNodeAt(coords.row, coords.col);
208
209         Vector3 pRotation = piece.transform.rotation.eulerAngles;
210         Quaternion newPRotation = Quaternion.Euler(pRotation.x, yRotation, pRotation.z);
211
212         GameObject pieceObject = Instantiate(piece, pieceNode.transform.position + Vector3.up *
1.2f, newPRotation) as GameObject;
213         pieceObject.transform.localScale = Vector3.zero;
//for scaling in start from zero
214         Piece pieceScript = pieceObject.GetComponent(
typeof(Piece)) as Piece;
215
216         
//assign mat and player type
217         Material mat =
null;
218         GCPlayer player =
null;
219         
switch (playerType) {
220             
case PlayerType.P1:
221                 mat = GameManager.Instance.PieceP1;
222                 player = GameManager.Instance.P1;
223                 
break;
224             
case PlayerType.P2:
225                 mat = GameManager.Instance.PieceP2;
226                 player = GameManager.Instance.P2;
227                 
break;
228         }
229         pieceObject.GetComponent<Renderer>().material = mat;
230         player.AddPieces(pieceScript);
231         pieceScript.PieceMovement = Creator.CreatePieceMovement(pieceScript.MovementType, player, pieceScript);
232         
233         
if(pieceScript) //if exists type then scale
234             pieceScript.ScaleIn(Random.Range(
0f,1f),Random.Range(1f,2f),piece.transform.localScale);
235         
236
237         pieceScript.UpdateNode(pieceNode);
238     }
239
240     
public void SpawnPiece(GridCoords coords, GameObject piece, PlayerType playerType) {
241         SpawnPiece(coords, piece,
0, playerType);
242     }
243
244     
public List<Node> GetNeighbours(Node node) {
245         List<Node> neighbours =
new List<Node>();
246         
for (int row = -1; row <= 1; row++) {
247             
for (int col = -1; col <= 1; col++) {
248
249                 
//skip these ones
250                 
if (row == 0 && col == 0)
251                     
continue;
252                 
if (!checkDiagonals && (row * row) == 1 && (col * col) == 1)
253                     
continue;
254
255                 
int checkRow = node.row + row;
256                 
int checkCol = node.col + col;
257
258                 
if (checkRow >= 0 && checkRow < rows && checkCol >= 0 && checkCol < cols) {
259                     neighbours.Add(grid[checkRow,checkCol]);
260                 }
261             }
262         }
263
264         
return neighbours;
265     }
266
267     
public bool IsNearby(Node nodeA, Node nodeB) {
268         
for (int row = -1; row <= 1; row++) {
269             
for (int col = -1; col <= 1; col++) {
270
271                 
//skip these ones
272                 
if (row == 0 && col == 0)
273                     
continue;
274                 
if (!checkDiagonals && (row * row) == 1 && (col * col) == 1)
275                     
continue;
276
277                 
int checkRow = nodeA.row + row;
278                 
int checkCol = nodeA.col + col;
279
280                 
if (checkRow >= 0 && checkRow < rows && checkCol >= 0 && checkCol < cols) {
281                     
if (grid[checkRow,checkCol] == nodeB) return true;
282                 }
283             }
284         }
285
286         
return false;
287     }
288
289     
void OnDrawGizmos() {
290         Gizmos.DrawWireCube(transform.position,
new Vector3(2,2,2));
291     }
292 }


[SerializeField]

private int height;

0 - box

1 - triangle

2 - circle

3 - cross

4 - hexagon

*----------------------CHESS sample

spawn circles

SpawnPiece(new GridCoords(1,i),piecesPrefabs[2], p1T); p1 circ

SpawnPiece(new GridCoords(6,i),piecesPrefabs[2], p2T); p2 circ

spawn boxes

SpawnPiece(new GridCoords(0,0),piecesPrefabs[0], p1T); p1 box

SpawnPiece(new GridCoords(0,7),piecesPrefabs[0], p1T); p1 box

SpawnPiece(new GridCoords(7,0),piecesPrefabs[0], p2T); p2 box

SpawnPiece(new GridCoords(7,7),piecesPrefabs[0], p2T); p2 box

spawn triangles

SpawnPiece(new GridCoords(0,2),piecesPrefabs[1], 180, p1T); p1 tri

SpawnPiece(new GridCoords(0,5),piecesPrefabs[1], 180, p1T); p1 tri

SpawnPiece(new GridCoords(7,2),piecesPrefabs[1], p2T); p2 tri

SpawnPiece(new GridCoords(7,5),piecesPrefabs[1], p2T); p2 tri

spawn crosses

SpawnPiece(new GridCoords(0,4),piecesPrefabs[3], p1T); p1 cross

SpawnPiece(new GridCoords(7,4),piecesPrefabs[3], p2T); p2 cross

spawn hexagons

SpawnPiece(new GridCoords(0,3),piecesPrefabs[4], p1T); p1 hex

SpawnPiece(new GridCoords(7,3),piecesPrefabs[4], p2T); p2 hex

spawn rectangles - knights for testing

SpawnPiece(new GridCoords(0,1),piecesPrefabs[5], p1T); p1 rect

SpawnPiece(new GridCoords(0,6),piecesPrefabs[5], p1T); p1 rect

SpawnPiece(new GridCoords(7,1),piecesPrefabs[5], p2T); p2 rect

SpawnPiece(new GridCoords(7,6),piecesPrefabs[5], p2T); p2 rect

*

SpawnPiece(new GridCoords(2,i),piecesPrefabs[2], p1T); p1 circ

SpawnPiece(new GridCoords(5,i),piecesPrefabs[2], p2T); p2 circ

spawn boxes

SpawnPiece(new GridCoords(1,1),piecesPrefabs[0], p1T); p1 box

SpawnPiece(new GridCoords(1,6),piecesPrefabs[0], p1T); p1 box

SpawnPiece(new GridCoords(6,1),piecesPrefabs[0], p2T); p2 box

SpawnPiece(new GridCoords(6,6),piecesPrefabs[0], p2T); p2 box

spawn triangles

SpawnPiece(new GridCoords(1,2),piecesPrefabs[1], 180, p1T); p1 tri

SpawnPiece(new GridCoords(1,5),piecesPrefabs[1], 180, p1T); p1 tri

SpawnPiece(new GridCoords(6,2),piecesPrefabs[1], p2T); p2 tri

SpawnPiece(new GridCoords(6,5),piecesPrefabs[1], p2T); p2 tri

spawn crosses

SpawnPiece(new GridCoords(1,4),piecesPrefabs[3], p1T); p1 cross

SpawnPiece(new GridCoords(6,4),piecesPrefabs[3], p2T); p2 cross

spawn hexagons

SpawnPiece(new GridCoords(1,3),piecesPrefabs[4], p1T); p1 hex

SpawnPiece(new GridCoords(6,3),piecesPrefabs[4], p2T); p2 hex

pieceObject.transform.localScale = Vector3.zero; for scaling in start from zero

assign mat and player type

if(pieceScript) if exists type then scale

skip these ones

skip these ones



Gõ tìm kiếm nhanh...